<script setup lang="ts">
import { RouterView } from 'vue-router'
import { ref, onMounted, onUnmounted } from 'vue'
import AppLoading from '@/components/AppLoading.vue'

// 应用初始化状态
const isAppReady = ref(false)
const loadingText = ref('正在初始化应用...')
const statusText = ref('请稍候')
const progress = ref(0)

// 事件监听器
let eventBus: any = null

onMounted(() => {
  // 获取全局事件总线
  eventBus = (window as any).eventBus

  if (eventBus) {
    // 监听加载状态更新
    eventBus.on('updateLoading', (data: any) => {
      if (data.text) loadingText.value = data.text
      if (data.status) statusText.value = data.status
      if (typeof data.progress === 'number') progress.value = data.progress
    })

    // 监听应用就绪状态
    eventBus.on('appReady', (ready: boolean) => {
      isAppReady.value = ready
    })
  }
})

onUnmounted(() => {
  // 清理事件监听器
  if (eventBus) {
    eventBus.callbacks = {}
  }
})
</script>

<template>
  <!-- 加载状态 -->
  <AppLoading
    v-if="!isAppReady"
    :loading-text="loadingText"
    :status-text="statusText"
    :progress="progress"
  />

  <!-- 应用主体 -->
  <RouterView v-else />
</template>

<style scoped>
/* App根组件样式 */
</style>
